package com.jwdf.uda.ui.fragment.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.CommonFragmentListBinding;
import com.jwdf.uda.view.XListView;

/**
 * Created by Administrator on 2016/5/14.
 */
public abstract class SimpleListFragment extends BaseFragment {

    protected XListView lv;
    protected CommonFragmentListBinding mDataBinding;
    private XListView.IXListViewListener mIXListener = new XListView.IXListViewListener() {
        @Override
        public void onRefresh(XListView xListView) {
            requestData(true);
        }

        @Override
        public void onLoadMore(XListView xListView) {
            requestData(false);
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = CommonFragmentListBinding.inflate(inflater, container, false);
        lv = mDataBinding.commonFragmentList;
        lv.addHeaderView(createHeaderView());
        lv.setPullRefreshEnable(canPullRefresh());
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onListItemClick(position - lv.getHeaderViewsCount());
            }
        });
        lv.setXListViewListener(mIXListener);

        initViews();
        requestData(true);

        return mDataBinding.getRoot();
    }
    protected View createHeaderView() {
        View headerView = getActivity().getLayoutInflater().inflate(R.layout.view_head_divider, null);
        return headerView;
    }
    protected boolean canPullRefresh() {
        return true;
    }
    protected abstract void initViews();
    protected abstract void onListItemClick(int position);
    protected abstract void requestData(boolean refresh);
    protected void autoRefresh() {
        mIXListener.onRefresh(mDataBinding.commonFragmentList);
    }
    @Override
    protected void onReload(Context context) {
        requestData(true);
    }

}
